﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using Nav3.UIExcel;
using BAL.EstRec;
using Nav3;
using System.Text.RegularExpressions;
using Nav3.PasteExcelData;

namespace MasterDetailEx
{
    /// <summary>
    /// Lógica de interacción para DialogGeneraGridExel.xaml
    /// </summary>
    public partial class DialogGeneraGridExel2 : Window
    {
        List<Pos> posCollection;
        List<Recurso> recursoCollection;
        List<CapturaPivotFDV> DatosIDS;
        List<int> filasColores;

        DataTable DatosCorrectos;
        DataTable DatosGrid;

        public CapturaFDV rfdv;
        public CapturaHq Hq;
        string Version;
        string currentPosition;
        bool IsHQ;

        public void setVersion(string Version)
        {

            this.Version = Version;
        }

        public void setCurrentPosition(string currentPosition)
        {

            this.currentPosition = currentPosition;
        }

        public void init(string clipboardData)
        {
            posCollection = Pos.GetComboPos();
            recursoCollection = Recurso.GetComboRecurso();
        }


        public void convertirExcelAGrid(string tabla)
        {
            DataTable ta;
            ta = TablaExcel.ExcelTabla(tabla);

            DataSet ds = new DataSet();

            int aux = 0;
            aux = ta.Rows.Count - 1;

            ds.Tables.Add(ta);

            ModelPasteExcelData mpe = new ModelPasteExcelData();

            DisparaEvento(ds.Tables[0]);
            dataGridExcel.ItemsSource = ds.Tables[0].DefaultView;
            
            //for (int i = 0; i < DatosGrid.Rows.Count; i++)
            //{
            //    DataRow drv;
            //    drv = DatosGrid.Rows[i];
            //    dataGridExcel.Items.Add(drv);
            //}
        }

        public void DisparaEvento(DataTable TodosLosDatos)
        {
            string sPOS = "";
            string sRecursos = "";
            bool aux = false;
            bool aux2 = false;

            posCollection = Pos.GetComboPos();
            recursoCollection = Recurso.GetComboRecurso();
            filasColores = new List<int>();

            if(!IsHQ)
                DatosIDS = CapturaPivotFDV.GetCapturaPivotFDV(long.Parse(Version), currentPosition);

            ModelPasteExcelData mpe = new ModelPasteExcelData();

            for (int i = 0; i < TodosLosDatos.Rows.Count; i++)
            {
                for (int j = 0;  j < TodosLosDatos.Columns.Count; j++)
                {
                    if (j == 0)
                    {

                        sPOS = TodosLosDatos.Rows[i][j].ToString();

                        if (!mpe.validarPOS(posCollection, sPOS))
                            aux = true;

                    }
                    else if (j == 1)
                    {

                        sRecursos = TodosLosDatos.Rows[i][j].ToString();

                        if (!mpe.validarRecurso(recursoCollection, sRecursos))
                            aux = true;

                    }
                    else if (j == 2)
                    {

                        if (!IsHQ)
                        {
                            if (!mpe.validaRecPos2FDV(DatosIDS, sPOS, sRecursos))
                                aux = true;
                        }
                        try
                        {
                            int valorCantidad = 0;
                            valorCantidad = Int32.Parse(TodosLosDatos.Rows[i][j].ToString());
                        }
                        catch (Exception Ex)
                        {
                            aux = true;
                            aux2 = true;
                        }

                        if (!aux)
                        {                          
                            DataRow rowrow = DatosCorrectos.NewRow();
                            rowrow[0] = "-1";
                            rowrow[1] = sPOS;
                            rowrow[2] = "-1";
                            rowrow[3] = sRecursos;
                            rowrow[4] = "-1";
                            rowrow[5] = TodosLosDatos.Rows[i][j].ToString();
                            rowrow[6] = Version;
                            DatosCorrectos.Rows.Add(rowrow);

                            filasColores.Add(10);

                            //DataRow rowrowG = DatosGrid.NewRow();
                            //rowrowG[0] = sPOS;
                            //rowrowG[1] = sRecursos;
                            //rowrowG[2] = DatosGrid.Rows[i][j].ToString();
                            //DatosGrid.Rows.Add(rowrowG);
                        }
                        else
                        {                                                    

                            if (!aux2)
                                filasColores.Add(30);
                            else
                                filasColores.Add(20);

                            aux = false;
                            aux2 = false; 
                        }
                    }
                }
            }
        }        
        


        public DialogGeneraGridExel2()
        {
            InitializeComponent();

            DatosCorrectos = new DataTable();

            DataColumn col01 = new DataColumn("UNID_HQBGT");
            DatosCorrectos.Columns.Add(col01);
            DataColumn col02 = new DataColumn("ID_POS");
            DatosCorrectos.Columns.Add(col02);
            DataColumn col03 = new DataColumn("POS");
            DatosCorrectos.Columns.Add(col03);
            DataColumn col04 = new DataColumn("ID_RECURSO");
            DatosCorrectos.Columns.Add(col04);
            DataColumn col05 = new DataColumn("RECURSO");
            DatosCorrectos.Columns.Add(col05);
            DataColumn col06 = new DataColumn("CANTIDAD");
            DatosCorrectos.Columns.Add(col06);
            DataColumn col07 = new DataColumn("UNID_HQBGT_VERSION");
            DatosCorrectos.Columns.Add(col07);

            this.IsHQ = false;
        }

        public DialogGeneraGridExel2(bool IsHQ)
        {
            InitializeComponent();

            DatosCorrectos = new DataTable();

            DataColumn col01 = new DataColumn("UNID_HQBGT");
            DatosCorrectos.Columns.Add(col01);
            DataColumn col02 = new DataColumn("ID_POS");
            DatosCorrectos.Columns.Add(col02);
            DataColumn col03 = new DataColumn("POS");
            DatosCorrectos.Columns.Add(col03);
            DataColumn col04 = new DataColumn("ID_RECURSO");
            DatosCorrectos.Columns.Add(col04);
            DataColumn col05 = new DataColumn("RECURSO");
            DatosCorrectos.Columns.Add(col05);
            DataColumn col06 = new DataColumn("CANTIDAD");
            DatosCorrectos.Columns.Add(col06);
            DataColumn col07 = new DataColumn("UNID_HQBGT_VERSION");
            DatosCorrectos.Columns.Add(col07);

            //DatosGrid = new DataTable();

            //DataColumn column = new DataColumn("pos"); ;
            //DatosGrid.Columns.Add(column);
            //DataColumn column2 = new DataColumn("Recursos"); ;
            //DatosGrid.Columns.Add(column2);
            //DataColumn column3 = new DataColumn("Valor"); ;
            //DatosGrid.Columns.Add(column3);

            this.IsHQ = true;
        }

        private void btnCancelar_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void dataGridExcel_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            int aux = e.Row.GetIndex();

            SolidColorBrush colorcito = new SolidColorBrush(Colors.White);

            if (filasColores[aux] == 20)
            {
                colorcito = new SolidColorBrush(Colors.Yellow);
            }
            else if (filasColores[aux] == 30) {

                colorcito = new SolidColorBrush(Colors.Tomato);
            }
            else if (filasColores[aux] == 10)
            {
                colorcito = new SolidColorBrush(Colors.White);
            }

            e.Row.Background = colorcito;
        }
      
        private void btnAgregar_Click(object sender, RoutedEventArgs e)
        {
            if (IsHQ)
                this.Hq.InformacionDeExcel(DatosCorrectos);
            else
                this.rfdv.InformacionDeExcel(DatosCorrectos);

            this.Close();
        }

        private void dataGridExcel_Loaded(object sender, RoutedEventArgs e)
        {

            try
            {
                dataGridExcel.Columns[0].Width = 125;
                dataGridExcel.Columns[1].Width = 125;
                dataGridExcel.Columns[2].Width = 125;
            }
            catch (Exception ex)
            {
                
                MessageBox.Show("No se pueden pegar datos del porta papeles.\nDetalles : " + ex.Message, "Pegar - loaded", MessageBoxButton.OK,MessageBoxImage.Warning);
            }
        }

        public void convertClipboardData(string clipboardData)
        {
            IDataObject data = Clipboard.GetDataObject();
            if (data.GetDataPresent(DataFormats.Text))
            {
                clipboardData = (String)data.GetData(DataFormats.Text);
            }

            string[] lines = Regex.Split(clipboardData, "\r\n");
            var res = lines.Select(d =>
            {
                CapturaPivotFDV cpf = null;
                try
                {
                    cpf = new CapturaPivotFDV(new Pos(d.Split('\t')[0], d.Split('\t')[0])
                                                     , new Recurso(d.Split('\t')[1], d.Split('\t')[1])
                                                     , Convert.ToInt16(d.Split('\t')[0].ToString()));
                }
                catch (Exception ex)
                {
                    ;
                }
                return cpf;
            });

            foreach (var r in res)
            {
                Console.WriteLine(r);
            }
        }
    }
}
